package com.czn.bookshop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.czn.bookshop.entity.Cart;
import com.czn.bookshop.entity.CartVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 购物车Mapper
 *
 * @author ChenZeNan
 * @create 2020-12-29 13:57
 */
@Repository
public interface CartMapper extends BaseMapper<Cart> {


    //根据用户查询购物车
    @Select("SELECT " +
            "bsc.*, bsb.NAME AS bookName, bsb.img_url AS img_url," +
            "bsb.new_price AS new_price " +
            " FROM " +
            "bs_cart bsc " +
            " LEFT JOIN bs_book bsb ON bsc.book_id = bsb.id " +
            " WHERE " +
            " bsc.user_id = #{userId} ")
    List<CartVo> findCartListByUserId(Integer userId);

    //根据记录id查询相应记录
    @Select({"<script>" +
                "SELECT " +
                " bsc.*, bsb.NAME AS bookName, bsb.img_url AS img_url, " +
                " bsb.new_price AS new_price " +
                " FROM " +
                " bs_cart bsc " +
                " LEFT JOIN bs_book bsb ON bsc.book_id = bsb.id " +
                " WHERE bsc.id in " +
                " <foreach item='item' collection='ids' open='(' separator=',' close=')' >" +
                    "#{item}" +
                "</foreach>" +
            "</script>"})
    List<CartVo> findCartListByIds(@Param("ids") List<String> ids);
}
